import axios from "axios";
import router from '../router'
import store from "../store";


export function login({dispatch}) {
    axios.get('/api/login-url', {
        params: {
            url: process.env['VUE_APP_URL'] + "/auth",
        }
    }).then(function (response) {
        window.location = response.data.data
    });
}

export function getToken({commit,dispatch}, ticket) {
    return axios.post('/api/login', {
        ticket: ticket,
    }).then(function (response) {
        if (!response.data.data.access_token) {
            alert("认证失败")
        }
        window.sessionStorage.setItem('auth-token', response.data.data.access_token)
        commit("token",response.data.data.access_token)
        router.push('/job')
    });
}

export function logout() {
    return axios.post('/api/logout', {
        url: process.env['VUE_APP_URL'],
    }).then(function (response) {
        window.sessionStorage.removeItem('auth-token')
        window.location = response.data.data;
    });
}

export function getAuthed({commit}) {
    return axios.get('/api/authed').then(function (response) {
        commit('authed', response.data)
    });
}
